package com.shen.core.data.dao;

import com.shen.core.data.mapper.BaseMapper;
import com.shen.core.data.model.BaseDO;
import com.shen.core.enums.DelEnum;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @Author: shenyang
 * @CreateTime: 2023-01-16  15:42
 * @Description: TODO
 * @Version: 1.0
 */
public abstract class BaseDAO<T extends BaseDO> {

    @Autowired
    private BaseMapper<T> baseMapper;

    public int save(T t) {
        return baseMapper.insertSelective(t);
    }


    public int saveAll(List<T> list) {
        return baseMapper.insertList(list);
    }

    public int delete(T t) {
        return baseMapper.delete(t);
    }


    public int deleteById(Integer integer) {
        return baseMapper.deleteByPrimaryKey(integer);
    }


    public int update(T t) {
        return baseMapper.updateByPrimaryKey(t);
    }

    public int updateAll(List<T> t) {
        return baseMapper.updateBatchByPrimaryKeySelective(t);
    }


    public T getById(Integer integer) {
        return baseMapper.selectByPrimaryKey(integer);
    }


    public List<T> getAll() {
        return baseMapper.selectAll();
    }

    public List<T> searchByExample(Example example) {
        return baseMapper.selectByExample(example);
    }

    public T selectOneByExample(Example example) {
        return baseMapper.selectOneByExample(example);
    }

    public T selectOneNotDelByExample(Example example) {
        example.and().andEqualTo(BaseDO.DEL, DelEnum.NOT_DEL);
        return baseMapper.selectOneByExample(example);
    }
}
